﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Net;
using BCL.Tool;
using System.IO;
using BCL.BaseLib;
using System.Threading.Tasks;

namespace BCL.Component
{
    public partial class IBList : DevExpress.XtraEditors.XtraUserControl
    {
        List<FunctionEntity> feList;
        public Action RefreshApp;
        /// <summary>
        /// 刷新按钮是否显示
        /// by ZhaoHuan
        /// </summary>
        //[Browsable(true)]
        //[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        //[EditorBrowsable(EditorBrowsableState.Always)]
        //public bool RefreshVisible
        //{
        //    get { return this.btnRefresh.Visible; }
        //    set { this.btnRefresh.Visible = value; }
        //}


        /// <summary>
        /// 是否自动隐藏
        /// </summary>
        public bool AutoVisible = true;

        public IBList()
        {
            InitializeComponent();
            // 初始化列表
            if (!this.DesignMode)
                Init();
        }

        public void Init()
        {
            try
            {
                xscMain.Controls.Clear();
                if (BCL.BaseLib.Basics.BCLApplication.AppFunTree == null)
                    return;

                this.feList = BCL.BaseLib.Basics.BCLApplication.AppFunTree;
                for (int i = feList.Count - 1; i >= 0; i--)
                {
                    var item = feList[i];
                    if (!item.IsCreate)
                        continue;
                    if ((item.ItemType & FunctionItemType.Group) != 0)
                    {
                        GroupControl gc = new GroupControl();
                        gc.Name = "gc" + i;
                        gc.Dock = DockStyle.Top;
                        gc.Text = item.Title;
                        if (item.Items != null)
                        {
                            var displayItems = item.Items.Where(m => m.IsCreate && m.IsHide != 1).ToList();
                            CreatBtn(gc, displayItems);
                        }
                        //this.Controls.Add(gc);
                        xscMain.Controls.Add(gc);
                        if (i != 0)
                        {
                            PanelControl pc = new PanelControl();
                            pc.Height = 5;
                            pc.Dock = DockStyle.Top;
                            xscMain.Controls.Add(pc);
                        }
                    }
                }

                xscMain.VerticalScroll.Value = 0;
            }
            catch (Exception)
            {
            }

        }

        private void CreatBtn(GroupControl gc, List<FunctionEntity> list)
        {
            #region 按钮生成参数
            int displayRowCount = 10; // 行个数
            #endregion
            int gcHeight = 0;
            for (int i = list.Count()-1; i >=0; i--)
            {
                FunctionEntity fe = list[i];
                ImageButton imgBtn = new ImageButton();
                imgBtn.Name = gc.Name + "Ib" + i;
                imgBtn.Tag = fe;
                imgBtn.Title = fe.Title.Language();
                imgBtn.BtnClicked += BaseLib.Basics.BCLApplication.MainForm.App_Click;

                imgBtn.BorderStyle = BorderStyle.None;
                if (fe.Image != null)
                    imgBtn.image = fe.Image;
                imgBtn.Dock = DockStyle.Left;
                //imgBtn.Location = new Point(5 + (i % displayRowCount) * imgBtn.Width, 5 + (i / displayRowCount) * imgBtn.Height);
                gc.Controls.Add(imgBtn);

                gcHeight = (list.Count() / displayRowCount + 1) * imgBtn.Height+15;
                // 提示
                //if (fe.Tip != null)
                //    AddTip(gi, fe.Tip.Title.Language(), fe.Tip.Text.Language());

                //if (fe.Image != null)
                //{
                //    gi.LargeImage = fe.Image;
                //}
                //else if (!string.IsNullOrEmpty(fe.ImagePath))
                //{
                //    gi.LargeImage = DownLoadImage(fe.ImagePath);
                //}
                ////gi.SmallImageSize = new System.Drawing.Size(20,20);
                //gi.Enabled = fe.Enabled;
                //group.ItemLinks.Add(gi);
                //this.Items.Add(gi);
            }
            gc.Height = gcHeight;
            //nbgContainer.OwnerGroup.GroupClientHeight = gcHeight;

        }



        private void IBList_Leave(object sender, EventArgs e)
        {
            if (AutoVisible)
            {
                this.Visible = false;
            }
        }

        private void IBList_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible)
            {
                this.Focus();
            }
        }

        private void IBList_Load(object sender, EventArgs e)
        {
            this.VerticalScroll.Value = 0;
        }


        private void btnRefresh_Click(object sender, EventArgs e)
        {
            //this.btnRefresh.Visible = false;
            //pnLoading.Visible = true;
            xscMain.Visible = false;
            xscMain.Controls.Clear();
            Application.DoEvents();
            this.Update();
            if (BCL.BaseLib.Basics.BCLApplication.MainForm.RefreshAppList != null)
                BCL.BaseLib.Basics.BCLApplication.MainForm.RefreshAppList.Invoke();
            else
                Init();
            //pnLoading.Visible = false;
            xscMain.Visible = true;
            //this.btnRefresh.Visible = true;
        }

        /// <summary>
        /// 刷新应用列表
        /// by ZhaoHuan
        /// </summary>
        public void RefreshAppList()
        {
            btnRefresh_Click(null, null);
        }
    }
}
